package edu.usm.cs.roberge.views;

import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.usm.cs.roberge.Message;
import edu.usm.cs.roberge.NewsReaderImpl;
import edu.usm.cs.roberge.NewsReaderModel;
import edu.usm.cs.roberge.workers.FetchMessageBodyWorker;
import edu.usm.cs.roberge.workers.FetchMessagesWorker;

public class FavoritesView extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;

	/**
	 * Create the frame.
	 * @param dataModel 
	 */
	public FavoritesView(final NewsReaderModel dataModel, final NewsReaderImpl impl) {
		setTitle("Favorites");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 987, 622);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblViewFavoriteNews = new JLabel("View Favorite News Groups");
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		JLabel lblMessageBody = new JLabel("Message Body:");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblViewFavoriteNews)
					.addContainerGap(828, Short.MAX_VALUE))
				.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 969, Short.MAX_VALUE)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 969, Short.MAX_VALUE)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(lblMessageBody)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(8)
					.addComponent(lblViewFavoriteNews)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE)
					.addGap(30)
					.addComponent(lblMessageBody)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE))
		);
		
		final JTextArea msgBody = new JTextArea();
		scrollPane_1.setViewportView(msgBody);
		msgBody.setLineWrap(true);
		
		final FavoritesTableModel tableModel = new FavoritesTableModel(new ArrayList<Message>());
		table = new JTable(tableModel);
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (!arg0.getValueIsAdjusting()) {
					// this method is actually invoked twice when a single row is
					// clicked by the user. On the second call, the valueIsAdjusting
					// property is set to false. Use this value as the flag to indicate
					// that the click selection has already been handled
					return;
				}
				
				final Message msg = tableModel.getMessageAtRow(table.getSelectedRow());
				if (msg == null) {
					return;
				}
				new FetchMessageBodyWorker(impl, msg) {
					@Override
					protected void done() {
						msgBody.setText(msg.getBody());
					}
				}.execute();
			}
			
		});
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
		
		new FetchMessagesWorker(dataModel, impl, tableModel).execute();
	}
}
